package com.wzw.expression.record.expression;

import com.wzw.expression.expression.operation.Operation;
import com.wzw.expression.expression.invocation.OperationInvocationMetadata;
import com.wzw.expression.expression.invocation.OperationInvocationMetadataProvider;
import org.springframework.beans.factory.BeanFactory;

import java.lang.reflect.Method;
import java.util.Objects;

/**
 * @author Wangzhiwen
 */
public class IRecordInvocationMetadataProvider implements OperationInvocationMetadataProvider {
    @Override
    public Class<? extends Operation> getType() {
        return IRecordOperation.class;
    }

    @Override
    public OperationInvocationMetadata get(Operation operation, Method method, Class<?> targetClass, BeanFactory beanFactory) {
        IRecordOperation recordOperation = (IRecordOperation) operation;
        IRecordInvocationMetadata metadata = new IRecordInvocationMetadata(recordOperation, method, targetClass);
        if (Objects.nonNull(beanFactory)) {
            metadata.setInterceptor(beanFactory.getBean(recordOperation.getInterceptorClass()));
        }
        return metadata;
    }
}
